package com.example.uitmanagement;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONObject;

import android.util.Log;

public class JsonParser {
	//-------node of MAP
	// XML node keys
	static final String KEY_subjectName = "subjectName"; // parent node
	static final String KEY_courseSize = "courseSize";
	static final String KEY_lecturerName = "lecturerName";
	static final String KEY_facultyID = "facultyID";
	static final String KEY_schedule = "schedule";
	static final String KEY_subjectTheory = "subjectTheory";
	//----String of node
	static  String subjectName = "subjectName"; // parent node
	static  String courseSize = "courseSize";
	static  String lecturerName = "lecturerName";
	static  String facultyID = "facultyID";
	static  String schedule = "schedule";
	static  String subjectTheory = "subjectTheory";
	// Key of JSon
	static final String courseStatus = "courseStatus";
	// contrustor
	public JsonParser() {
		// TODO Auto-generated constructor stub
	}
	public JSONArray GetJSONArray(){		
		String readTwitterFeed = readTwitterFeed();					
			try
	        {
				JSONObject json = new JSONObject(readTwitterFeed);
				JSONArray jsonArray = json.getJSONArray("data");	
				return jsonArray;
	        }
	        catch (Exception je)
	        {	          
	        	return null;
	        }	  				
	}
	public String readTwitterFeed() {
	    StringBuilder builder = new StringBuilder();
	    HttpClient client = new DefaultHttpClient();
	    HttpGet httpGet = new HttpGet("http://uitstudentmanagement.appspot.com/course?type=full");
	    try {
	        HttpResponse response = client.execute(httpGet);
	        StatusLine statusLine = response.getStatusLine();
	        int statusCode = statusLine.getStatusCode();
	        if (statusCode == 200) {
	            HttpEntity entity = response.getEntity();
	            InputStream content = entity.getContent();
	            BufferedReader reader = new BufferedReader(new InputStreamReader(content));
	            String line;
	            while ((line = reader.readLine()) != null) {
	                builder.append(line);
	            }
	        } else {
	            Log.e(JsonParser.class.toString(), "Failed to download file");
	        }
	    } catch (ClientProtocolException e) {
	        e.printStackTrace();
	    } catch (IOException e) {
	        e.printStackTrace();
	    }
	    return builder.toString();
	}
	public ArrayList<HashMap<String, String>> getMap()
	{
		ArrayList<HashMap<String, String>> ArrayMap = new ArrayList<HashMap<String, String>>();
		JSONArray jsonArray = GetJSONArray();
		// looping through all jsonarray nodes 
		for (int i = 0; i < jsonArray.length(); i++) {
			try {
				//create new JsonObject	
				Log.e(JsonParser.class.toString(), "Failed to add map2");
				JSONObject js = jsonArray.getJSONObject(i);
				Log.e(JsonParser.class.toString(), "Failed to add map3");
				// creating new HashMap
				HashMap<String, String> map = new HashMap<String, String>();			
				// adding each child node to HashMap key => value
				subjectName		=js.getString("courseID")+"_"+js.getString("subjectName");
				Log.e(JsonParser.class.toString(), "Failed to add map5");
				courseSize 		=js.getString("courseSize")+"/"+js.getString("courseMaxSize");
				Log.e(JsonParser.class.toString(), "Failed to add map6");
				lecturerName	=js.getString("lecturerName");
				Log.e(JsonParser.class.toString(), "Failed to add map7");
				facultyID		="Khoa :"+js.getString("facultyID");	
				Log.e(JsonParser.class.toString(), "Failed to add map8");
				schedule		="Thu "+js.getString("courseDay")+ "Ca "+js.getString("courseTime")+"Phong "+js.getString("roomID");
				Log.e(JsonParser.class.toString(), "Failed to add map9");
				subjectTheory	="TCLT :"+js.getString("subjectTheory") +"TH :"+js.getString("subjectPractice");
				Log.e(JsonParser.class.toString(), "Failed to add map10");
				Log.e(JsonParser.class.toString(), "Failed to add map1");
				map.put(KEY_subjectName, subjectName);	
				map.put(KEY_courseSize, courseSize);	
				map.put(KEY_facultyID, facultyID);	
				map.put(KEY_lecturerName, lecturerName);	
				map.put(KEY_subjectTheory, subjectTheory);	
				map.put(KEY_schedule, schedule);				
				ArrayMap.add(map);	
			} catch (Exception e) {
				// TODO: handle exception
				Log.e(JsonParser.class.toString(), "Failed to add map");
			}
			
		}		
		return ArrayMap;
	}
}
